import {_decorator, Button, Component, director, resources, Sprite, SpriteFrame, Node} from "cc";
import {lobby} from "db://assets/script/lobby";
import {PropBag} from "db://assets/script/game/mainui/props/prop-bag";
import {userInfo} from "db://assets/script/user-info";
import {BuildingBox} from "db://assets/script/game/city/building-box";
import {BagType} from "db://assets/script/game/constants/bag-type";
import {ZoneMapPlane} from "db://assets/script/game/mainui/battle/zone-map-plane";
import {GameEvent} from "db://assets/script/game-event";
import {UserFishFryComeEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-come-event";
import {MessageBox} from "db://assets/script/game/mainui/city/message-box";

const {ccclass, property} = _decorator;

@ccclass('UiInCity')
export class UiInCity extends Component {
    @property(Button)
    propBagBtn!: Button

    @property(Button)
    editChangeBtn!: Button

    @property(Button)
    zoneMapBtn!: Button

    @property(BuildingBox)
    buildingSelector!: BuildingBox

    @property(MessageBox)
    messageBox!: MessageBox

    private _editChangeIconBase = 'textures/ui-icon/'

    start() {
        this._refreshEditBtnIcon()
        this.propBagBtn.node.on(Button.EventType.CLICK, this._showPropBag, this)
        this.editChangeBtn.node.on(Button.EventType.CLICK, this._changeEditStatus, this)
        this.zoneMapBtn.node.on(Button.EventType.CLICK, () => {
            const zoneMapPlaneNode = lobby.instance.addUIWithClickMask('prefab/common/zone-map-plane', 0, (node) => node.destroy())
            zoneMapPlaneNode.getComponent(ZoneMapPlane)!.init(null)
        })
        director.emit(GameEvent.userFishRequestFishFryCome, new UserFishFryComeEvent())
    }

    onEnable() {
        this._refreshEditBtnIcon()
    }

    _showPropBag() {
        const fishBagUI = lobby.instance.addUIWithClickMask("prefab/common/fish-bag", 0, (node) => node.destroy())
        const pg = fishBagUI.getComponent(PropBag)!
        pg.showAssetsBag(BagType.PlayerBag, userInfo.assetInfo!.assets, null)
    }

    _changeEditStatus() {
        userInfo.cityInfo.changeInEdit()
        director.emit(GameEvent.cityChangeInEdit, userInfo.cityInfo.checkInEdit())
        this._refreshEditBtnIcon()
    }

    _refreshEditBtnIcon() {
        this.editChangeBtn.getComponent(Sprite)!.spriteFrame = resources.get(this._editChangeIconBase + (userInfo.cityInfo.checkInEdit() ? 'edit-complete' : 'begin-edit') + '/spriteFrame', SpriteFrame)!
        this.buildingSelector.node.active = userInfo.cityInfo.checkInEdit()
    }
}